//
//  MemberChangePasswordController.m
//  yameishe3
//
//  Created by pro on 2017/3/2.
//  Copyright © 2017年 yameishe. All rights reserved.
//

#import "MemberChangePasswordController.h"
#import "YMSMemberManager.h"
#import "LoginViewController.h"
@interface MemberChangePasswordController ()
@property(weak, nonatomic)IBOutlet UITextField *oldPasswordTF;
@property(weak, nonatomic)IBOutlet UITextField *changePasswordTF;
@property(weak, nonatomic)IBOutlet UITextField *confirmPasswordTF;
@property(weak, nonatomic)IBOutlet UIButton *functionBtn;


@end

@implementation MemberChangePasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)viewWillDisappear:(BOOL)animated {
    // 反注册键盘
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)onTouchFunctionBtn:(id)sender
{
    if ([self checkFunctionState]==NO) {
        return;
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[YMSMember loginMember].token forKey:@"_token"];
    [dic setValue:@"update_pass" forKey:@"type"];
    [dic setValue:_oldPasswordTF.text forKey:@"old_pass"];
    [dic setValue:_changePasswordTF.text forKey:@"new_pass"];
    [dic setValue:_confirmPasswordTF.text forKey:@"pass_confirm"];
    [[YMSMemberManager sharedInstance] MemberUpdateInfoWithDic:dic successBlock:^(NSDictionary *result) {
        UIStoryboard *main = [UIStoryboard storyboardWithName:@"LoginViewController" bundle:nil];
        LoginViewController *loginVc = [main instantiateInitialViewController];
        [UIApplication sharedApplication].keyWindow.rootViewController = loginVc;
        [YMSMember setLoginMember:nil];
        [YMSMember saveLoginMember];
//        [self.navigationController popViewControllerAnimated:YES];
    } failureBlock:^(NSString *error) {
        
    }];
}

-(BOOL)checkFunctionState
{
    if (!_oldPasswordTF.text.length) {
        NSLog(@"");
        return NO;
    }
    if (!_changePasswordTF.text.length) {
        NSLog(@"");
        return NO;
    }
    if (!_confirmPasswordTF.text.length) {
        NSLog(@"");
        return NO;
    }
    if (![_changePasswordTF.text isEqualToString:_confirmPasswordTF.text]) {
        NSLog(@"两次输入密码不一致");
        return NO;
    }
        return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
